import rclpy
from rclpy.node import Node
from std_msgs.msg import String
from rclpy.qos import DurabilityPolicy
from rclpy.qos import HistoryPolicy
from rclpy.qos import QoSProfile
from rclpy.qos import LivelinessPolicy
from rclpy.qos import ReliabilityPolicy
from rclpy.qos_event import PublisherEventCallbacks


class MinimalPublisher(Node):

    def __init__(self):
        qos = QoSProfile(depth=10)
        #设置不同的qos策略
        qos.history =HistoryPolicy.RMW_QOS_POLICY_HISTORY_KEEP_ALL
        custom_callback = lambda event: print("Sever Incompatible Callback!")
        callbacks = PublisherEventCallbacks()
        callbacks.incompatible_qos = custom_callback

        super().__init__('minimal_publisher')

        self.publisher_ = self.create_publisher(String, 'topic', qos,event_callbacks=callbacks)     # CHANGE
        timer_period = 0.5
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0

    def timer_callback(self):
        msg = String()                                           # CHANGE
        msg.data = '%d'%self.i                                      # CHANGE
        self.publisher_.publish(msg)
        self.get_logger().info('Publishing: %s' % msg.data)  # CHANGE
        self.i += 1


def main(args=None):
    rclpy.init(args=args)

    minimal_publisher = MinimalPublisher()

    rclpy.spin(minimal_publisher)

    minimal_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()
